﻿using System.Collections.Generic;
using TwitterBootstrapMVC;
using Web.UI.AdminLTE.Common;
using Web.UI.AdminLTE.Enums;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Controls.Simple
{
    public class AlertLte : HtmlElementBase
    {
        private readonly string _alerthtml;
        private bool _closeable;
        private Icon _icon;

        public AlertLte(string alertHtml, AlertType alertType = AlertType.Danger)
        {
            _alerthtml = alertHtml;
            _closeable = false;
            EnsureClass("alert", alertType.GetEnumDescription());
            SetDefaultAlertIcon(alertType);
        }

        private void SetDefaultAlertIcon(AlertType alertType)
        {
            switch (alertType)
            {
                case AlertType.Danger:
                    Icon("fa fa-ban");
                    break;
                case AlertType.Info:
                    Icon("fa fa-info");
                    break;
                case AlertType.Success:
                    Icon("fa fa-check");
                    break;
                case AlertType.Warning:
                    Icon("fa fa-warning");
                    break;
            }
        }

        public AlertLte Closeable()
        {
            _closeable = true;
            return this;
        }

        public AlertLte IsLongMessage()
        {
            EnsureClass("alert-block");
            return this;
        }

        public AlertLte Icon(Icon icon)
        {
            _icon = icon;
            return this;
        }

        public AlertLte Icon(string icon)
        {
            return Icon(new Icon(icon));
        }

        public override string ToHtmlString()
        {
            var tag = TagBuilderExtensions.New("div")
                .MergeHtmlAttributes(HtmlAttr.FormatHtmlAttributes())
                .Html(string.Format("{0}{1}", _closeable
                        ? "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>"
                        : string.Empty, _alerthtml));
            if (_icon != null)
            {
                tag.PrependHtml(_icon);
            }
            return tag.ToString();
        }

        //  Common methods for all html elements
        #region Common Methods
        public AlertLte HtmlAttributes(IDictionary<string, object> htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public AlertLte HtmlAttributes(object htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public AlertLte Id(string id)
        {
            SetId(id);
            return this;
        }

        public AlertLte Class(string cssClass)
        {
            EnsureClass(cssClass);
            return this;
        }

        public AlertLte Data(object htmlDataAttributes)
        {
            MergeHtmlDataAttributes(htmlDataAttributes);
            return this;
        }

        #endregion
    }
}